<?php


namespace app\admin\controller;


use think\Exception;
use think\facade\View;
use app\admin\validate\Category as CategoryValidate;
use app\admin\business\Category as CategoryBus;
use app\common\lib\Status as StatusLib;
class Category extends AdminBase
{
    public function index(){
        $pid = input('param.id',0,'intval');
        $data = [
            'pid' => $pid
        ];
        try {
            $categories = (new CategoryBus()) ->getLists($data,5);
//            halt($categories);
//            exit();
        }
        catch (\Exception $e){
            $categories = [];
        }
        return View::fetch('',[
            'categories' => $categories
        ]);
    }

    public function add(){
        try {
            $categories = (new CategoryBus())->getNormalCategories();
        }catch (\Exception $e){
            $categories = [];
        }
        return View::fetch('',[
            'categories' => json_encode($categories)
        ]);
    }

    public function save() {
        $pid = input('param.pid',0,'intval');
        $name = input('param.name','','trim');
        $data = [
            'pid' => $pid,
            'name' => $name
        ];
        $validate = new CategoryValidate();
        if (!$validate->scene('save')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        try {
            $result = (new CategoryBus())->add($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        if ($result) {
            return show(config('status.success'), "OK");
        }else{
            return show(config('status.error'),'新增分类失败');
        }
    }

    // 排序的修改
    public function listorder(){
        $id = input('param.id',0,'intval');
        $listorder = input('param.listorder',0,'intval');
        $data = [
            'id' => $id,
            'listorder' => $listorder
        ];
        $validate = new CategoryValidate();
        if (!$validate->scene('listorder')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        try {
            $res = (new CategoryBus())->listorder($id,$listorder);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        if (!$res){
            return show(config('status.error'),'排序失败');
        }else{
            return show(config('status.success'),'排序成功',$res);
        }
    }


    /**
     * 更改状态
     */

    public function status(){
        $status = input('param.status',0,'intval');
        $id = input('param.id',0,'intval');
        $data = [
            'status' => $status,
            'id' =>  $id
        ];
        $validate = new CategoryValidate();
        if (!$validate->scene('status')->check($data)){
            var_dump($validate->getError());
            return show(config('status.error'),$validate->getError());
        }
        if (!$id || !in_array($status,StatusLib::getTableStatus())){
            return show(config('status.error'),'参数错误');
        }

        try {
            $res = (new CategoryBus())->status($id,$status);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        if ($res){
            return show(config('status.success'),'状态更新成功');
        }else {
            return show(config('status.error'),'状态更新失败');
        }
    }

    /**
     * 删除
     */
    public function delete(){
        $id = input('param.id',0,'intval');
        try {
            $res = (new CategoryBus())->del($id);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        if ($res){
            return show(config('status.success'),'删除成功');
        }else {
            return show(config('status.error'),'删除失败');
        }
    }


}